package com.explorex.infra.puma.gateway.netty.rrmp;


import com.explorex.infra.puma.gateway.netty.common.AbstractBaseServer;
import com.explorex.infra.puma.gateway.netty.common.SpringContextUtils;
import com.explorex.infra.puma.gateway.netty.redis.BaseSessionRedis;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.AdaptiveRecvByteBufAllocator;
import io.netty.channel.ChannelOption;
import io.netty.channel.WriteBufferWaterMark;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.flush.FlushConsolidationHandler;
import io.netty.handler.traffic.ChannelTrafficShapingHandler;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;


/**
 * @author: JacceYang
 * @createTime: 2022/08/06 14:20
 * @description: rrmp protocal server
 */
@Slf4j
public class RTCPServer extends AbstractBaseServer {

    @Setter
    @Getter
    private static ApplicationContext context;
    @Setter
    @Getter
    private RTcpSocketEvent socketEvent;

    @Getter
    private BaseSessionRedis baseSessionRedis= SpringContextUtils.getBean("baseSessionRedis",BaseSessionRedis.class);


    @Override
    protected void initSocketChannel(SocketChannel ch) {
        super.initSocketChannel(ch);
//        ch.pipeline().addLast(new TcpChannelManagerHandler(baseSessionRedis, this));
//        ch.pipeline().addLast("ChannelTrafficShapingHandler-Duplex", new ChannelTrafficShapingHandler(4 * 1024 * 1024L, 2 * 1024 * 1024L));
//        ch.pipeline().addLast("ConnectorEncoder-Out", new ConnectorEncodeHandler());
//        ch.pipeline().addLast("AESEncryptHandler", new AESEncryptHandler());
//        ch.pipeline().addLast("ProxyProtocolHandler", new ProxyProtocolHandler());
//        ch.pipeline().addLast("ConnectorDecoder-In", new ConnectorLengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 6, 2, 4, 0));
//        ch.pipeline().addLast("AESDecryptHandler", new AESDecryptHandler());
//        ch.pipeline().addLast("FlushConsolidationHandler-Duplex",new FlushConsolidationHandler(5));
//        // 端上的心跳上报时间间隔为210s, readIdle 需要小于此值才能踢掉没有权限的连接
//        ch.pipeline().addLast("IdleStateHandler-Duplex", new WatchDogHandler(300, 300, 300));
//        ch.pipeline().addLast("RTCPServerHandler-In", new RTCPServerHandler(this, socketEvent));
    }

    @Override
    protected void initServerBootStrap(ServerBootstrap serverBootstrap) {
        super.initServerBootStrap(serverBootstrap);

        serverBootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .option(ChannelOption.SO_KEEPALIVE, true)
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .option(ChannelOption.SO_REUSEADDR, true)
                .childOption(ChannelOption.SO_REUSEADDR, true)
                .option(ChannelOption.TCP_NODELAY, true)
                .childOption(ChannelOption.TCP_NODELAY, true)
                .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(64*1024 * 1024, 128 * 1024 * 1024))
                .option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT);
    }
}
